package cn.itcast.bos.service.take_delivery.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.take_delivery.WayBillRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.service.take_delivery.WayBillService;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {
	@Resource
	private WayBillRepository repository;
	@Resource
	private WayBillIndexRepository indexRepository;

	@Override
	public void save(WayBill model) {
		// 初始化运单状态为1:代发货
		model.setSignStatus(1);
		repository.save(model);
		indexRepository.save(model);
	}

	@Override
	public WayBill findByWayBillNum(String wayBillNum) {
		return repository.findByWayBillNum(wayBillNum);
	}

	@Override
	public Page<WayBill> findpageQuery(WayBill wayBill, Pageable pageable) {
		// 判断waybill是否存在条件
		if (StringUtils.isBlank(wayBill.getWayBillNum()) && StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress()) && StringUtils.isBlank(wayBill.getSendProNum())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {
			// 无条件查询
			return repository.findAll(pageable);
		} else {
			// 有条件查询,must == and , should == or ,
			// 创建布尔查询对象,多条件拼接
			BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
			// 添加查询条件
			if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				// 运单号查询
				QueryBuilder termQuery = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
				queryBuilder.must(termQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {
				// 发货地模糊查询
				// 情况一 :查询词条的一部分,使用模糊查询
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("sendAddress", wayBill.getSendAddress());
				// 情况二 : 多个词条的组合
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getSendAddress())
						.field("sendAddress").defaultOperator(Operator.AND);
				// 用or连接两者关系
				BoolQueryBuilder query = new BoolQueryBuilder();
				query.should(wildcardQuery);
				query.should(queryStringQueryBuilder);
				queryBuilder.must(query);
			}
			if (StringUtils.isNoneBlank(wayBill.getRecAddress())) {
				// 发货地模糊查询
				// 情况一 :查询词条的一部分,使用模糊查询
				QueryBuilder wildcardQuery = new WildcardQueryBuilder("recAddress", wayBill.getRecAddress());
				// 情况二 : 多个词条的组合
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(wayBill.getRecAddress())
						.field("recAddress").defaultOperator(Operator.AND);
				// 用or连接两者关系
				BoolQueryBuilder query = new BoolQueryBuilder();
				query.should(wildcardQuery);
				query.should(queryStringQueryBuilder);
				queryBuilder.must(query);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				// 速运类型,等值查询
				QueryBuilder termQuery = new TermQueryBuilder("sendProNum", wayBill.getSendProNum());
				queryBuilder.must(termQuery);
			}
			if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
				// 速运类型,等值查询
				QueryBuilder termQuery = new TermQueryBuilder("signStatus", wayBill.getSignStatus());
				queryBuilder.must(termQuery);
			}
			SearchQuery searchQuery = new NativeSearchQuery(queryBuilder);
			// 分页效果
			searchQuery.setPageable(pageable);
			return indexRepository.search(searchQuery);
		}

	}

	// 更新运单信息到es索引库的方法
	@Override
	public void syncIndex() {
		List<WayBill> wayBills = repository.findAll();
		indexRepository.save(wayBills);
	}

	@Override
	public List<WayBill> findWayBills(WayBill wayBill) {
		// 判断WayBill 中条件是否存在
		if (StringUtils.isBlank(wayBill.getWayBillNum())
				&& StringUtils.isBlank(wayBill.getSendAddress())
				&& StringUtils.isBlank(wayBill.getRecAddress())
				&& StringUtils.isBlank(wayBill.getSendProNum())
				&& (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {
			// 无条件查询 、查询数据库
			return repository.findAll();
		} else {
			// 查询条件
			// must 条件必须成立 and
			// must not 条件必须不成立 not
			// should 条件可以成立 or
			BoolQueryBuilder query = new BoolQueryBuilder(); // 布尔查询 ，多条件组合查询
			// 向组合查询对象添加条件
			if (StringUtils.isNoneBlank(wayBill.getWayBillNum())) {
				// 运单号查询
				QueryBuilder tempQuery = new TermQueryBuilder("wayBillNum",
						wayBill.getWayBillNum());
				query.must(tempQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendAddress())) {
				// 发货地 模糊查询
				// 情况一： 输入"北" 是查询词条一部分， 使用模糊匹配词条查询
				QueryBuilder wildcardQuery = new WildcardQueryBuilder(
						"sendAddress", "*" + wayBill.getSendAddress() + "*");

				// 情况二： 输入"北京市海淀区" 是多个词条组合，进行分词后 每个词条匹配查询
				QueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(
						wayBill.getSendAddress()).field("sendAddress")
						.defaultOperator(Operator.AND);

				// 两种情况取or关系
				BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
				boolQueryBuilder.should(wildcardQuery);
				boolQueryBuilder.should(queryStringQueryBuilder);

				query.must(boolQueryBuilder);
			}
			if (StringUtils.isNoneBlank(wayBill.getRecAddress())) {
				// 收货地 模糊查询
				QueryBuilder wildcardQuery = new WildcardQueryBuilder(
						"recAddress", "*" + wayBill.getRecAddress() + "*");
				query.must(wildcardQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				// 速运类型 等值查询
				QueryBuilder termQuery = new TermQueryBuilder("sendProNum",
						wayBill.getSendProNum());
				query.must(termQuery);
			}
			if (StringUtils.isNoneBlank(wayBill.getSendProNum())) {
				// 速运类型 等值查询
				QueryBuilder termQuery = new TermQueryBuilder("sendProNum",
						wayBill.getSendProNum());
				query.must(termQuery);
			}
			if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
				// 签收状态查询
				QueryBuilder termQuery = new TermQueryBuilder("signStatus",
						wayBill.getSignStatus());
				query.must(termQuery);
			}

			SearchQuery searchQuery = new NativeSearchQuery(query);
			// ElasticSearch 允许搜索分页查询，最大数据条数10000
			Pageable pageable = new PageRequest(0, 10000);
			searchQuery.setPageable(pageable);// 分页效果

			// 有条件查询 、查询索引库
			return indexRepository.search(searchQuery).getContent();
		}
	}
}
